home *** CD-ROM | disk | FTP | other *** search
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 1
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IMaking The Jump To C++~Y~I
-
- Throughout this book, we've tried to use ideas that are normally only
- found in object oriented languages such as C++. We've been creating
- objects using C's structures, and hiding the implementation of the objects
- with macro interfaces. This is very much like what the class does for
- programmers in C++. As you can see, we don't have to limit object oriented
- techniques to object oriented languages.
-
- #WN
- Using structures with macro interfaces has made the programs we've
- developed a bit wordy. However, I've found that this style of programming
- helps make the transition to C++ much easier for the students in my C
- courses. More and more, new development is being done in object oriented
- languages such as C++.
-
- #WN
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 2
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~ITrue Objects~Y~I
-
- The objects, such as attackers or polygons, that we've defined for our
- games aren't objects in the truest sense of the objects found in object
- oriented languages. They lack a number of features that are present in C++
- classes. To be specific, the C objects that we've used have no ~R~Iprivate
- data, no member functions, no inheritance, and no polymorphism~Y~I.
-
-
- ~W~IPrivate Data~Y~I
-
- Objects in C++ are created by creating classes. An example of a simple
- class, a point, is shown in Figure 10.1 on the next page. A C++ class has
- members just like a C structure. However, unlike a structure, the section
- that contains the data in a class is usually private. Being private, the
- data may only be accessed by member functions (which are discussed below).
- Because of this, access to an object's data is very controlled.
- #WP
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 3
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IFigure 13.1~Y~I
- A Simple C++ Class
-
- class point
- {
- private:
- int x,y;
- public:
- void set_row(int row) {y=row;}
- void set_col(int col) (x=col;}
- int get_row(void) {return y;}
- int get_col(void) {return x;}
- };
-
-
- #WP
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 4
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~IThe great advantage of a C++ class over a C structure is that a C++ class
- forces programmers to use the member functions to access data. The C
- structures we implemented had macro interfaces, but there's no way to
- force the maintenance programmer to use the macros. He/she could easily
- bypass our wonderful interfaces and completely destroy the re-usability of
- our carefully crafted code.
-
- #WN
- Having the data private helps hide the implementation of the class. The
- implementation can be changed quite radically without affecting the rest
- of the program as long as the interface to the class, which is formed by
- the member functions, stays the same.
-
- #WP
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 5
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IMember Functions~Y~I
-
- As the example above shows, C++ classes not only have member data, but
- member functions as well. These functions are usually part of the public
- section of a class. They are used to provide the set of all valid
- operations that may be done on an object. Each type of object has its own
- member functions.
-
- #WN
- Encapsulating member functions with member data makes C++ objects more of
- a single organic unit. That helps to make code more portable and re-
- usable. It also helps to isolate errors and simplify the debugging
- process, which is usually not a fun task.
-
- #WP
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 6
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~Inheritance~Y~I
-
- Our objects have acquired the characteristics of other objects by including
- a member in the structure of the other object. For instance, a space
- attacker acquired the attributes of an object of type character by having a
- member of type character in the attacker. This is not really inheritance,
- it's actually called containership. But it's as close as C can get to
- inheritance.
-
- #WN
- True inheritance means that the inheriting object is also partially
- composed of the inherited object. An example is given in Figure 10.2.
- #WP
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 7
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IFigure 10.2~Y~I
- Inheritance
-
- class point
- {
- private:
- int x,y;
- public:
- void set_row(int row) {y=row;}
- void set_col(int col) (x=col;}
- int get_row(void) {return y;}
- int get_col(void) {return x;}
- }
- #C1,R23
- ~C~IContinued On Next Page~Y~I
-
- #WP
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 8
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IFigure 10.2 (cont)~Y~I
- Inheritance
-
- class pixel : public point
- {
- private:
- int color;
- public:
- get_color(void) {return color;}
- void set_color(int new_color) {color=new_color;}
- }
-
- #WP
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 9
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~I
- In this example we've created a new object called a pixel. A pixel itself
- has only one piece of member data, the color. However, the statement
- public point, on the first line of the pixel class definition means that a
- pixel inherits all of the functionality of a point. All of the
- functionality means just that. It inherits both member data and member
- functions. That means that code can be reused very easily. We can quickly
- build very complex objects from much simpler ones without having to
- rewrite code.
-
- #WP
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 10
- #HS,1,4,80,25,11,1
- #C4,R5
- ~W~IPolymorphism~Y~I
-
- Polymorphism is a four-bit word for a two-bit idea. What it means is that,
- in object oriented languages like C++, the programmer has the ability to
- define many functions with the same name. The advantage of this is not
- immediately obvious until you look at a few examples.
-
- #WN
- C doesn't support strings, at least not to the extent that languages such
- as Basic and Pascal do. In Pascal, you can make the statement
- str1=str2+str3, where str1, str2, and str3 are strings. The result of the
- statement is that the concatenation of str2 and str3 is stored in str1. It
- would be nice to be able to add this same ability to C's strings.
-
- #WP
- %
- #EF
- #T15,1,Chapter 10 Some Final Thoughts Pg. 11
- #HS,1,4,80,25,11,1
- #C4,R5
- ~Y~I
- In C++, this is easy to do. Since we can have many functions with the same
- name, polymorphism, we can write a new function called + that works for
- strings. If we wanted to, we could also define a + function for points,
- polygons, and segments. We can have as many versions of + as we want
- to. The C++ compiler knows what type of objects we're adding together, so
- it calls the appropriate + function for that type of object.
-
- #WN
- ~W~IThe Long And Short Of Objects~Y~I
-
- As I've stated, I developed the style of programming that I've presented
- to help make the transition to C++ easier. I hope that you will seriously
- consider making that transition as soon as possible. The advanced language
- features you acquire when you move to object oriented languages will enable
- you to build very complex games, and other types of programs, with much
- greater ease than is otherwise possible.
- #WP
- #X